package cn.hp.controller;

import cn.hp.entity.DictType;
import cn.hp.service.DictTypeService;
import cn.hp.util.Result;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/api/dictType")
public class DictTypeController {
    /**
     * 200 连接成功
     * 400 连接失败
     * 404 无法找到请求的资源（页面），说明前端url存在问题 （html有问题）
     * 500 服务器端出现错误， 说明后端代码存在问题 （java有问题）
     */


    @Autowired
    private DictTypeService dictTypeService;

    /**
     * 查询字典类型
     * @param dictType
     * @return
     */
    @GetMapping
    public Result find(DictType dictType){
        List<DictType> dictTypeList = dictTypeService.selectDictType(dictType);
        return Result.success(dictTypeList);
    }

    @RequestMapping("/page")
    public Result page(DictType dictType, @RequestParam(value = "page", defaultValue = "1") Integer page,@RequestParam(value = "limit", defaultValue = "10") Integer limit) {
        PageInfo<DictType> dictTypePageInfo = dictTypeService.selectDictType(dictType, page, limit);
        return Result.success(dictTypePageInfo);
    }

    @PostMapping
    public Result insert(@RequestBody DictType dictType) {
        int i = dictTypeService.insertDictType(dictType);
        return i > 0 ? Result.success("添加成功") : Result.error("添加失败");
    }

    @PutMapping
    public Result update(@RequestBody DictType dictType) {
        int i = dictTypeService.updateDictType(dictType);
        return i > 0 ? Result.success("修改成功") : Result.error("修改失败");
    }

    @DeleteMapping("/{id}")
    public Result delete(@PathVariable Integer id ) {
        int i = dictTypeService.deleteDictTypeById(id);
        return i > 0 ? Result.success("删除成功") : Result.error("删除失败");
    }


}